import 'dart:io';

import 'package:flutter/material.dart';

import 'package:image_cropper/image_cropper.dart';

/// 裁剪比例
class ImageCropperRatio {
  final double ratioX;
  final double ratioY;

  ImageCropperRatio({required this.ratioX, required this.ratioY});
}

/// 图片裁剪
class ImageCropperUtil {
  static Future<File?> crop(
    File file, {
    int? maxWidth,
    int? maxHeight,
    ImageCropperRatio? aspectRatio,
    ImageCompressFormat compressFormat = ImageCompressFormat.jpg,
    int compressQuality = 90,
    String? title = '图片裁剪',
    CropStyle cropStyle = CropStyle.rectangle,
  }) async {
    CroppedFile? res = await ImageCropper().cropImage(
      sourcePath: file.absolute.path,
      maxWidth: maxWidth,
      maxHeight: maxHeight,
      aspectRatio: aspectRatio == null
          ? null
          : CropAspectRatio(
              ratioX: aspectRatio.ratioX,
              ratioY: aspectRatio.ratioY,
            ), // 裁剪比例
      compressFormat: compressFormat,
      compressQuality: compressQuality,
      uiSettings: [
        IOSUiSettings(
          title: title,
          doneButtonTitle: '完成',
          cancelButtonTitle: '取消',
          cropStyle: cropStyle,
          aspectRatioPickerButtonHidden: true, // 隐藏比例选择
          resetButtonHidden: true, // 隐藏重置按钮
          rotateButtonsHidden: true, // 隐藏旋转
        ),
        AndroidUiSettings(
          toolbarTitle: title,
          backgroundColor: Colors.black,
          hideBottomControls: true,
          cropStyle: cropStyle,
        ),
      ],
    );
    if (res != null) {
      return File(res.path);
    }
    return null;
  }
}
